<?php

namespace Home\Controller;

//use Think\Controller;

class GradeController extends ExtendController {

    //显示栏目
    public function index() {
        $Grade = D('Grade');
        $Questions = D('Questions');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Grade->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Grade->order('date DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key => $value) {
            //print_r($list);
            $map['q_id'] = $value['qid'];
            $data = $Questions->field('q_content, q_answer')->where($map)->find();
            $value['content'] = $data['q_content'];
            if ($value['answer_a'] == $data['q_answer']) {
                $res = '正确';
            } else {
                if (empty($value['ansert_a']) && !empty($value['answer_b'])) {
                    $res = '';
                } else {
                    $res = '不正确';
                }
            }
            //$content = $data['q_content'];
            //$list[$key] = $content; 
            $value['lai'] = $res;
            $list[$key] = $value;
        }

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display(); // 输出模板
    }

    //添加
    public function add() {
        $Subject = M('Subject');
        $data['name'] = $_POST['column_name'];
        $data['image'] = $_POST['column_link'];
        $result = $Subject->add($data);
        $jsonResult = array();  //定义数组
        if ($result != false) {
            $jsonResult["flag"] = true;
        } else {
            $jsonResult["flag"] = false;
        }
        echo json_encode($jsonResult);
    }

    //修改
    public function save() {
        $Subject = M('Subject');
        $data['id'] = $_POST['column_id'];
        $column = $Subject->where($data)->find();
        if (!empty($column)) {
            $data['name'] = $_POST['column_name'];
            $data['image'] = $_POST['column_link'];
            $result = $Subject->save($data);
            $jsonResult = array();  //定义数组
            if ($result != false) {
                $jsonResult["flag"] = true;
            } else {
                $jsonResult["flag"] = false;
            }
        } else {
            $jsonResult["flag"] = false;
        }
        echo json_encode($jsonResult);
    }

    //删除
    public function delete() {
        $Grade = M('Grade');
        $map['id'] = $_GET['id'];
        $result = $Grade->where($map)->delete();
        if ($result != false) {
            $this->redirect('index');
        } else {
            $this->error('删除失败!');
        }
    }

    //显示应聘个人信息
    public function message() {
        $memberModel = M('Member');
        $subjectModel = M('Subject');
        import('ORG.Util.Page'); // 导入分页类
        $count = $memberModel->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        $list = $memberModel->field("id,name member_name,sid,phone,date")->limit($Page->firstRow, $Page->listRows)->select();

        foreach ($list as $key => $v) {
            $subject = $subjectModel->field("name")->where(array("id" => $v["sid"]))->find();
            $v["name"] = $subject["name"];
            $list[$key] = $v;
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display('message');
    }

    //显示个人成绩
    public function look_grade() {
        $Grade = D('Grade');
        $Questions = D('Questions');
        $memberModel = D("Member");
        if (!empty($_GET["type"])) {
            $map["q_type"] = $_GET["type"];
        }
        if (!empty($_GET["judge"])) {
            $map["judge"] = $_GET["judge"];
        }
        $map['mid'] = $_GET['uid'];
        $map['sid'] = $_GET['sid'];
        import('ORG.Util.Page'); // 导入分页类
        $count = $Grade->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        $list = $Grade->order('date DESC')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $num = 0;
        $num2 = 0;
        $username = "";
        foreach ($list as $key => $value) {
            //print_r($list);
            $map2['q_id'] = $value['qid'];
            $data = $Questions->field('q_content, q_answer')->where($map2)->find();
            $member = $memberModel->where(array("id" => $map['mid']))->find();
            $value['content'] = $data['q_content'];
            $value["user_name"] = $member["name"];
            $username = $value["user_name"];
            $value["user_phone"] = $member["phone"];
            //先判断是否为选择题
            if ($value["q_type"] == 1) {
                if ($value['answer_a'] == $data['q_answer']) {
                    $num = $num + 1;
                    $res = '正确';
                    $Grade->where(array("id" => $value["id"]))->save(array("judge" => 1));
                } else {
                    $Grade->where(array("id" => $value["id"]))->save(array("judge" => 2));
                    $num2 = $num2 + 1;
                    $res = '不正确';
                }
            } else {
                $res = '待评价';
            }
            $value['lai'] = $res;
            $list[$key] = $value;
        }
        $this->assign('num2', $num2);
        $this->assign('num', $num);
        $this->assign('username', $username);
        $this->assign('uid', $map["mid"]);
        $this->assign('sid', $map["sid"]);
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display('index'); // 输出模板
    }

    //显示填空题问题答案
    public function essayQuestion() {
        $map['username'] = $_GET['name'];
        $map['q_type'] = 2;
        $Grade = D('Grade');
        $Questions = D('Questions');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Grade->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Grade->order('date DESC')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key => $value) {
            //print_r($list);
            $map['q_id'] = $value['qid'];
            $data = $Questions->field('q_content, q_answer')->where($map)->find();
            $value['content'] = $data['q_content'];
            $list[$key] = $value;
        }
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display('index'); // 输出模板
    }

    //显示选择题问题答案
    public function choose() {
        $map['username'] = $_GET['name'];
        $map['q_type'] = 1;
        $Grade = D('Grade');
        $Questions = D('Questions');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Grade->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Grade->order('date DESC')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $num = 0;
        $num2 = 0;
        foreach ($list as $key => $value) {
            //print_r($list);
            $map['q_id'] = $value['qid'];
            $data = $Questions->field('q_content, q_answer')->where($map)->find();
            $value['content'] = $data['q_content'];
            if ($value['answer_a'] == $data['q_answer']) {
                $num = $num + 1;
                $res = '正确';
            } else {
                if (empty($value['ansert_a']) && !empty($value['answer_b'])) {
                    $res = '';
                } else {
                    $res = '不正确';
                    $num2 = $num2 + 1;
                }
            }
            $value['lai'] = $res;
            $list[$key] = $value;
        }
        $this->assign('num2', $num2);
        $this->assign('num', $num);
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display('index'); // 输出模板
    }


    //搜索查询
    public function search() {
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $GradeModel = M('Grade');
        $Subject = M('Subject');
       $memberModel = D('Member');
       $subjectModel = D('Subject');

        $map["date"] = array(between, array($start_time, $end_time));

            $list = $memberModel->field("id,name member_name,sid,phone,date")->where($map)->order('date desc')->select();
            if(!empty($list)){
            foreach ($list as  $key=>$v) {
                $subject = $subjectModel->field("name")->where(array("id" => $v["sid"]))->find();
                $v["name"] = $subject["name"];
                $list[$key] = $v;
            }
            $this->assign('list', $list);
            $this->assign('list', $list);
            $this->display('message');
          }else{
            $this->error('没有人来面试');
          }
    }

}